import axios from "axios";

const baseUrl='/api'

// aixos二次封装的核心逻辑
class httpRequest {
    constructor(baseUrl){
        this.baseUrl=baseUrl
    }
    getInsideConfig(){
        const config={
            baseUrl:this.baseUrl,
            header:{}
        }
        return config
    }

    interception(instance){
        // 请求拦截
        instance.interceptors.request.use(function (config){
            return config
        },function (error) {
            return Promise.reject(error)
        })

        // 响应拦截
        instance.interceptors.response.use(function(response){
            return response
        },function(error){
            return Promise.reject(error)
        })
    }

    request(options){
        options={...this.getInsideConfig(),...options}
        
        // 创建axios实例
        const instance = axios.create()
        // 实例拦截器的绑定
        this.interception(instance)
        return instance(options)
    }
}

export default new httpRequest(baseUrl)